//app.js
App({
  globalData: {
    userInfo: null,
    user_id: null,
    hostName: 'https://www.daliandaxue.cn',//https://www.daliandaxue.cn   //http://192.168.0.102:8080
    authflag: false
  },
  onLaunch: function (options) {

    console.log("[onLaunch] 场景值:", options.scene)
    wx.setStorageSync('scene', options.scene);
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.setStorageSync('hostName', 'https://www.daliandaxue.cn');
    // 登录
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          console.log("code " + res.code)
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.getUserInfo({
            success: function (getUserInfoResponse) {
              var r = getUserInfoResponse;
              var hostname = wx.getStorageSync('hostName');
              wx.request({
                url: hostname + '/microappointment/user/loginByWx?code=' + code,
                method: 'get',
                header: {
                  'content-type': 'application/json'
                },
                success: function (response) {
                  wx.setStorageSync('user_id', response.data.data.userId);
                  wx.setStorageSync('user_nickName', r.userInfo.nickName);
                  wx.setStorageSync('user_openid', response.data.data.openid);
                  var hostname = wx.getStorageSync('hostName');
                  wx.request({
                    url: hostname + '/microappointment/user/init',
                    method: 'post',
                    header: {
                      'content-type': 'application/json'
                    },
                    data: {
                      id: response.data.data.userId,
                      username: r.userInfo.nickName,
                      imageUrl: r.userInfo.avatarUrl,
                      openid: response.data.data.openid,
                      city: r.userInfo.city,
                      country: r.userInfo.country,
                      province: r.userInfo.province,
                      gender: r.userInfo.gender
                    },
                    success: function (message) {
                      console.log('microappointment/user/init成功')
                      var hostname = wx.getStorageSync('hostName');
                      wx.request({
                        url: hostname + '/microappointment/user/find?userId='
                        + response.data.data.userId,
                        method: 'get',
                        header: {
                          'content-type': 'application/json'
                        },
                        success: function (message) {
                          console.log('message login...', message)
                          // 解密成功
                          if (message.data) {
                            var userInfo_ = message.data;
                            console.log(userInfo_)
                          } else {
                            console.log('解密失败')
                          }
                        },
                        fail: function () {
                          console.log('系统错误')
                        }
                      })
                    },
                    fail: function () {
                      console.log('microappointment/user/update错误')
                    }
                  })

                },
                fail: function () {
                  console.log('系统错误')
                }
              })
            },
            fail: function (res) {
              
              wx.showModal({
                title: '提示',
                content: '您取消了授权，这会导致部分功能异常，请先删除本小程序后重新加载并授权。',
                success: function (res) {
                  console.log("success")
                  wx.navigateTo({
                    url: '/pages/exit/exit',
                  }) 
                },
                fail: function() {
                  wx.navigateTo({
                    url: '/pages/exit/exit',
                  }) 
                }
              })
            }

          })

        } else {
          console.log("获取用户登录失败" + res.errMsg);
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  }
})